package com.vgooo.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.vgooo.bean.NewListInfo;
import com.vgooo.bean.NewsPing;
import com.vgooo.bean.ProdAbstract;
import com.vgooo.bean.UserInfo;

/**
 * JSon数据解析工具
 * 
 * @author Administrator
 * 
 */
public class JsonParserUtils {

	private final static String className = "JsonParserUtils";

	/**
	 * 解析Json格式的评论数据
	 * 
	 * @param jsonString
	 * @return List<NewsPing> 评论列表
	 */
	public static List<NewsPing> getNewsPings(String jsonString) {
		List<NewsPing> newsPings = null;
		List<Map<String, Object>> feedback = null;
		try {
			if (jsonString == null || "".equals(jsonString)) {
				return null;
			}
			feedback = GSonTools.getMulObjectList(jsonString);
			newsPings = getNewsPings(feedback);
		} finally {
			jsonString = null;
			feedback = null;
			System.gc();
		}
		return newsPings;
	}

	/**
	 * 解析已进行初步处理的评论数据
	 * 
	 * @param feedback
	 *            List<Map<String,Object>> 已经初步解析的Json数据
	 * @return List<NewsPing> 评论列表
	 */
	public static List<NewsPing> getNewsPings(List<Map<String, Object>> feedback) {
		if (feedback == null || feedback.isEmpty() || feedback.size() < 1) {
			return null;
		}
		DebugTools.log(className, feedback.toString());
		List<NewsPing> list = null;
		list = new ArrayList<NewsPing>();
		for (Map<String, Object> map : feedback) {
			NewsPing news = new NewsPing();
			news.news_id = (String) map.get("news_id");
			if (news.news_id == null || "".equals(news.news_id)) {
				news.news_id = (String) map.get("album_id");
			}
			news.feedback_id = (String) map.get("feedback_id");
			news.user_name = (String) map.get("user_name");
			news.user_img = (String) map.get("user_img");
			news.feedback_content = (String) map.get("feedback_content");
			news.feedback_time = (String) map.get("feedback_time");
			news.parent_id = (String) map.get("parent_id");
			// DebugTools.log(className, "parent_id = " +news.parent_id);
			Object o = map.get("parent");
			if (news.parent_id != null && !"0".equals(news.parent_id)
					&& !(o instanceof Boolean)) {
				Map<String, String> p = (Map<String, String>) o;
				// DebugTools.log(className, p.toString());
				news.parent = new NewsPing();
				news.parent.feedback_id = p.get("feedback_id");
				news.parent.user_id = p.get("user_id");
				news.parent.user_name = p.get("user_name");
				news.parent.feedback_content = p.get("feedback_content");
				p = null;
			} else {
				news.parent_id = "0";
			}
			list.add(news);
			o = null;
			map = null;
		}
		feedback = null;
		return list;

	}
	/**
	 * 解析已进行初步处理的评论数据
	 * 
	 * @param feedback
	 *            List<Map<String,Object>> 已经初步解析的Json数据
	 * @return List<NewsPing> 评论列表
	 */
	public static List<NewsPing> getVideoPings(List<Map<String, Object>> feedback) {
		if (feedback == null || feedback.isEmpty() || feedback.size() < 1) {
			return null;
		}
		DebugTools.log(className, feedback.toString());
		List<NewsPing> list = null;
		list = new ArrayList<NewsPing>();
		for (Map<String, Object> map : feedback) {
			NewsPing news = new NewsPing();
			news.news_id = (String) map.get("v_id");
			news.feedback_id = (String) map.get("comment_id");
			news.user_name = (String) map.get("user_name");
			news.user_id = (String) map.get("user_id");
			news.user_img = (String) map.get("user_img");
			news.feedback_content = (String) map.get("comment_content");
			news.feedback_time = (String) map.get("comment_time");
			news.parent_id = (String) map.get("parent_id");
			// DebugTools.log(className, "parent_id = " +news.parent_id);
			Object o = map.get("parent");
			if (news.parent_id != null && !"0".equals(news.parent_id)
					&& !(o instanceof Boolean)) {
				Map<String, String> p = (Map<String, String>) o;
				// DebugTools.log(className, p.toString());
				news.parent = new NewsPing();
				news.parent.feedback_id = p.get("comment_id");
				news.parent.user_id = p.get("user_id");
				news.parent.user_name = p.get("user_name");
				news.parent.feedback_content = p.get("comment_content");
				p = null;
			} else {
				news.parent_id = "0";
			}
			list.add(news);
			o = null;
			map = null;
		}
		feedback = null;
		return list;
		
	}

	/**
	 * 解析Json格式的评论数据
	 * 
	 * @param jsonString
	 * @return List<NewsPing> 评论列表
	 */
	public static List<NewsPing> getProdPings(String jsonString) {
		List<NewsPing> newsPings = null;
		List<Map<String, Object>> feedback = null;
		try {
			if (jsonString == null || "".equals(jsonString)) {
				return null;
			}
			feedback = GSonTools.getMulObjectList(jsonString);
			newsPings = getProdPings(feedback);
		} finally {
			jsonString = null;
			feedback = null;
			System.gc();
		}
		return newsPings;
	}

	/**
	 * 解析已进行初步处理的评论数据
	 * 
	 * @param feedback
	 *            List<Map<String,Object>> 已经初步解析的Json数据
	 * @return List<NewsPing> 评论列表
	 */
	public static List<NewsPing> getProdPings(List<Map<String, Object>> feedback) {
		if (feedback == null || feedback.isEmpty() || feedback.size() < 1) {
			return null;
		}
		DebugTools.log(className, feedback.toString());
		List<NewsPing> list = new ArrayList<NewsPing>();
		for (Map<String, Object> map : feedback) {
			NewsPing news = new NewsPing();
			news.news_id = (String) map.get("p_id");
			// if(news.news_id==null||"".equals(news.news_id)){
			// news.news_id = (String) map.get("album_id") ;
			// }
			news.feedback_id = (String) map.get("comment_id");
			news.user_name = (String) map.get("user_name");
			news.user_img = (String) map.get("user_img");
			news.feedback_content = (String) map.get("comment_content");
			news.feedback_time = (String) map.get("comment_time");
			news.parent_id = (String) map.get("parent_id");
			// DebugTools.log(className, "parent_id = " +news.parent_id);
			Object o = map.get("parent");
			if (news.parent_id != null && !"0".equals(news.parent_id)
					&& !(o instanceof Boolean)) {
				Map<String, String> p = (Map<String, String>) o;
				// DebugTools.log(className, p.toString());
				news.parent = new NewsPing();
				news.parent.feedback_id = p.get("feedback_id");
				news.parent.user_id = p.get("user_id");
				news.parent.user_name = p.get("user_name");
				news.parent.feedback_content = p.get("comment_content");
				p = null;
			} else {
				news.parent_id = "0";
			}
			list.add(news);
			o = null;
			map = null;
		}
		return list;

	}

	/**
	 * 解析已进行初步处理的评论数据
	 * 
	 * @param feedback
	 *            List<Map<String,Object>> 已经初步解析的Json数据
	 * @return List<NewsPing> 评论列表
	 */
	public static NewListInfo getNews(Map<String, Object> map) {

		NewListInfo video = new NewListInfo();
		Map<String, Object> newsMap;
		try {
			video.news_id = (String) map.get("news_id");
			newsMap = (Map<String, Object>) map.get("news");
			if ("0".equals(video.news_id)) {
				video.newsType = NewListInfo.NEWS_TYPE_ALBUM;
				video.news_id = (String) newsMap.get("album_id");
				video.news_title = (String) newsMap.get("album_title");
				video.news_source = (String) newsMap.get("album_source");
				// video.news_author =(String) map.get("news_author");
				video.news_time = (String) newsMap.get("album_time");
				video.news_views = (String) newsMap.get("album_views");
				video.news_thumbnail = (String) newsMap.get("album_thumbnail");
				// video.news_small_thumbnail =(String)
				// map.get("news_small_thumbnail");
				video.news_abstract = (String) newsMap.get("album_abstract");
				video.news_ping = (String) newsMap.get("news_ping");
				// video.news_views =(String) map.get("news_views");
				video.picture = new ArrayList<String>();
				List<Map<String, String>> p = (List<Map<String, String>>) newsMap
						.get("pictures");
				int len = p.size();
				for (int i = 0; i < len; i++) {
					video.picture.add(p.get(i).get("image_small_thumbnail"));
				}
				p = null;
			} else {
				video.newsType = NewListInfo.NEWS_TYPE_NEWS;
				video.news_title = (String) newsMap.get("news_title");
				video.news_source = (String) newsMap.get("news_source");
				// video.news_author =(String) map.get("news_author");
				video.news_time = (String) newsMap.get("news_time");
				video.news_views = (String) newsMap.get("news_views");
				video.news_thumbnail = (String) newsMap.get("news_thumbnail");
				// video.news_small_thumbnail =(String)
				// map.get("news_small_thumbnail");
				video.news_abstract = (String) newsMap.get("news_abstract");
				video.news_ping = (String) newsMap.get("news_ping");
				// video.news_views =(String) map.get("news_views");
			}
		} finally {

			newsMap = null;
			map = null;
		}
		return video;

	}

	/**
	 * 解析已进行初步处理的评论数据
	 * 
	 * @param feedback
	 *            List<Map<String,Object>> 已经初步解析的Json数据
	 * @return List<NewsPing> 评论列表
	 */
	public static List<NewListInfo> getNews(List<Map<String, Object>> list) {

		ArrayList<NewListInfo> news = new ArrayList<NewListInfo>();
		try {
			for (Map<String, Object> map : list) {
				news.add(getNews(map));
			}
		} finally {
			list = null;
		}
		return news;

	}

	/**
	 * 解析已进行初步处理的评论数据
	 * 
	 * @param feedback
	 *            List<Map<String,Object>> 已经初步解析的Json数据
	 * @return List<NewsPing> 评论列表
	 */
	public static List<ProdAbstract> getProds(List<Map<String, Object>> list) {

		List<ProdAbstract> prodsInfo = new ArrayList<ProdAbstract>(list.size());
		try {
			for (Map<String, Object> map : list) {
				prodsInfo.add(getProd(map));
			}
		} finally {
			list = null;
			System.gc();
		}
		return prodsInfo;

	}

	public static ProdAbstract getProd(Map<String, Object> map) {
		ProdAbstract prod = new ProdAbstract();
		try {
			prod.brand_html = (String) map.get("brand_html");
			prod.brand_name = (String) map.get("brand_name");
			prod.prod_html = (String) map.get("prod_html");
			prod.prod_name = (String) map.get("prod_name");
			prod.prod_thumbnail = (String) map.get("prod_thumbnail");
			prod.price_sell = (String) map.get("lower_price");
		} finally {
			map = null;
		}
		return prod;
	}

	/**
	 * 
	 * @param jsonString
	 * @return 用户信息
	 */
	public static UserInfo getUserInfo(String jsonString) {
		return getUserInfo(GSonTools.getObjectMaps(jsonString));
	}

	/**
	 * 
	 * @param map
	 * @return 用户信息
	 */
	public static UserInfo getUserInfo(Map<String, Object> map) {
		UserInfo userInfo = new UserInfo();
		Map<String, String> m = (Map<String, String>) map.get("user");
		userInfo.user_id = m.get("user_id");
		userInfo.unser_name = m.get("user_name");
		userInfo.user_email = m.get("user_email");
		m = (Map<String, String>) map.get("profile");
		userInfo.nick_name = m.get("nick_name");
		userInfo.gender = m.get("gender");
		userInfo.shop_face = m.get("shop_face");
		userInfo.address = m.get("address");
		userInfo.effect_cash = m.get("effect_cash");
		userInfo.user_ping_count = (String) map.get("user_ping_count");
		return userInfo;
	}

	
	/**
	 * V频道新闻列表json数据解析
	 * @param list
	 * @return
	 */
	public static List<NewListInfo> getVchannlList(List<Map<String, Object>> list) {
		List<NewListInfo> news = new ArrayList<NewListInfo>();
		for (Map<String, Object> m : list) {
			String type = (String) m.get("type");
			Map<String, Object> m2 = (Map<String, Object>) m.get("arr");
			NewListInfo newInfo = new NewListInfo();
			news.add(newInfo);
			newInfo.news_ping = (String) m.get("ping_count");
			if ("n".equals(type)) {
				newInfo.newsType = NewListInfo.NEWS_TYPE_NEWS;
				newInfo.news_id = (String) m2.get("news_id");
				newInfo.news_title = (String) m2.get("news_title");
				newInfo.news_source = (String) m2.get("album_source");
				newInfo.news_thumbnail = (String) m2.get("news_thumbnail");
				newInfo.news_abstract = (String) m2.get("news_abstract");
			} else if ("p".equals(type)) {
				newInfo.newsType = NewListInfo.NEWS_TYPE_ALBUM;
				newInfo.news_id = (String) m2.get("album_id");
				newInfo.news_title = (String) m2.get("album_title");
				newInfo.news_thumbnail = (String) m2.get("album_thumbnail");
				newInfo.news_abstract = (String) m2.get("album_abstract");
				newInfo.picture = new ArrayList<String>();
				List<Map<String, String>> pictures = (List<Map<String, String>>) m2
						.get("pictures");
				for (Map<String, String> m3 : pictures) {
					newInfo.picture.add(m3.get("image_small_thumbnail"));
				}

			} else if ("v".equals(type)) {
				newInfo.newsType = NewListInfo.NEWS_TYPE_VIDEO;
				newInfo.news_id = (String) m2.get("v_id");
				newInfo.news_title = (String) m2.get("v_title");
				newInfo.news_thumbnail = (String) m2.get("v_images");
				newInfo.news_abstract = (String) m2.get("v_desc");
				newInfo.v_source_url = (String) m2.get("v_source_url");
			}
		}
		return news;
	}

}
